#include <string>
#include <map>
#include <iostream>


struct Person {
    Person() : year(0), money(0) {}

    std::string name;
    int year;
    int money;
};


Person parse(std::string str) {
    Person result;

    for (int i = 0; i < str.find(" "); i++) {
        result.name += str[i];
    }

    str.erase(0, str.find(" ") + 1);

    for (int i = 0; i < str.find(" "); i++) {
        result.year = result.year * 10 + str[i] - 48;
    }

    str.erase(0, str.find(" ") + 1);

    for (int i = 0; i < str.length(); i++) {
        result.money = result.money * 10 + str[i] - 48;
    }

    return result;
}


class Taxpayers {
private:
    std::map<std::string, std::map<int, int> > data;
public:
    void add(std::string name, int year, int money) {
        data[name][year] = money;
    }


    void add(std::string str) {
        Person currentPerson;
        currentPerson = parse(str);
        data[currentPerson.name][currentPerson.year] = currentPerson.money;
    }


    void decorPrinter(int count) {
        for (int i = 0; i < count; i++)
            std::cout << "=";
        std::cout << std::endl << std::endl;
    }


    void printAll() {
      std::cout << std::endl << "===== General report =====" << std::endl;
      for (std::map<std::string, std::map<int, int> >::iterator it = data.begin(); it != data.end(); ++it)
        {
            std::cout << it->first << std::endl;
            std::map<int, int> temp = it->second;
            for (std::map<int, int>::iterator it2 = temp.begin(); it2 != temp.end(); ++it2) {
              std::cout << it2->first << ": $" << it2->second << std::endl;
            }
        }
        decorPrinter(26);
    }


    void printAbout(std::string name) {
        std::cout << std::endl << "===== " << name << " report =====" << std::endl;
        std::map<int, int> temp = data[name];
        for (std::map<int, int>::iterator it = temp.begin(); it != temp.end(); ++it) {
            std::cout << it->first << ": $" << it->second << std::endl;
        }
        decorPrinter(19 + name.length());
    }


    double getMiddleIncomePerYear(int year) {
        int count = 0;
        double mean = 0;
        for (std::map<std::string, std::map<int, int> >::iterator it = data.begin(); it != data.end(); ++it)
          {
              std::map<int, int> temp = it->second;
              for (std::map<int, int>::iterator it2 = temp.begin(); it2 != temp.end(); ++it2) {
                 if (year == it2->first) {
                     mean += it2->second;
                     count++;
                 }
              }
          }
          return mean = mean / count;
    }

};


int main() {
    Taxpayers taxpayers;
    taxpayers.add("Dima", 2016, 500);
    taxpayers.add("Dima 2015 500");
    taxpayers.add("Vova 2015 400");
    taxpayers.add("Pasha 2015 550");
    taxpayers.add("Vlad 2015 0");
    taxpayers.printAll();
    std::cout << "Middle Income Per 2015 Year: " << taxpayers.getMiddleIncomePerYear(2015) << std::endl;
    taxpayers.printAbout("Vlad");
}
